﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VkClientIM.UI;
using Settings = VkClientIM.Properties.Settings;

namespace VkClientIM
{
	/// <summary>
	/// Interaction logic for ClientDesktop.xaml
	/// </summary>
	public partial class ClientDesktop : Window
	{
		private MainWindow mainWindow;
		private Splash splash;
		public ClientDesktop()
		{
			InitializeComponent();


			

			CommandBindings.Add(new CommandBinding(ApplicationCommands.Close,
									   delegate { this.Close(); }));
			CommandBindings.Add( new CommandBinding( NavigationCommands.FirstPage,
			                                         delegate( object sender,
			                                                   ExecutedRoutedEventArgs e )
			                                         	{
			                                         		switch ( Visibility )
			                                         		{
			                                         			case Visibility.Visible:
			                                         				Visibility = Visibility.Hidden;
			                                         				break;
			                                         			case Visibility.Hidden:
			                                         				Visibility = Visibility.Visible;
			                                         				Activate();
			                                         				break;
			                                         		}
			                                         	} ) );



		}

		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			Settings.Default.MainWindowX = (double) mainWindow.GetValue(Canvas.LeftProperty);
			Settings.Default.MainWindowY = (double) mainWindow.GetValue(Canvas.TopProperty);
			Settings.Default.MainWindowSizeX = mainWindow.Width;
			Settings.Default.MainWindowSizeY = mainWindow.Height;
			Settings.Default.Save();



			if (!mainWindow.AllowClose)
			{
				e.Cancel = true;
				Hide();
			}
			else
			{
				foreach (Window window in App.Current.Windows)
				{
					if (window!=this)
					{
						window.Close();
					}
				}
				App.Current.Shutdown();
			}

			base.OnClosing(e);
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			splash=new Splash();
			double y = (Root.ActualHeight - splash.Height) / 2;
			double x = (Root.ActualWidth - splash.Width) / 2;
			splash.SetValue(Canvas.LeftProperty, x);
			splash.SetValue(Canvas.TopProperty, y);
			splash.SplashCompleted += new EventHandler(splash_SplashCompleted);
			Root.Children.Add( splash );
		}

		void splash_SplashCompleted(object sender, EventArgs e)
		{
			Dispatcher.Invoke(
				new Action( delegate
					{
						Root.Children.Remove( splash );
						splash.Visibility = Visibility.Hidden;
						mainWindow = new MainWindow();
						mainWindow.AttachSession( splash.GetSession() );
						splash = null;
						mainWindow.Width = Settings.Default.MainWindowSizeX;
						mainWindow.Height = Settings.Default.MainWindowSizeY;
						mainWindow.SetValue( Canvas.LeftProperty, Settings.Default.MainWindowX );
						mainWindow.SetValue( Canvas.TopProperty, Settings.Default.MainWindowY );
						Root.Children.Add( mainWindow );
					} 
					) );
		}
	}
}
